using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;
using PeopleSearchPivot;

namespace PeopleSearchPivot.Features.PivotJitServer
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("2636a426-df71-4970-83e3-2862a5bef99b")]
    public class PivotJitServerEventReceiver : SPFeatureReceiver
    {
        
     #region Constant      
        private const string FeatureId = "be5593ba-2c46-43af-a308-5ab8683b950b";
        private const string CxmlExt = "*.cxml";
        private const string DzcExt = "*.dzc";
        private const string DziExt = "*.dzi";
        private const string DeepZoomImageHandlerPath = "*/dzi/*_files/*/*_*.jpg";
        private const string ImageTileHandlerPath = "*_files/*/*_*.jpg";

    #endregion

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

            foreach (SPWebConfigModification mod in CreateConfigModifications())
            {
                webApp.WebConfigModifications.Add(mod);
            }

            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            webApp.Update();
        }


        

       public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
       {
           SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

           foreach (SPWebConfigModification mod in CreateConfigModifications())
           {
               webApp.WebConfigModifications.Remove(mod);
           }

           webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
           webApp.Update();
       }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        private List<SPWebConfigModification> CreateConfigModifications()
        {
            List<SPWebConfigModification> configModifications = new List<SPWebConfigModification>();

            SPWebConfigModification handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/handlers";
            string CxmlHandlerType = typeof(CxmlHandler).AssemblyQualifiedName;
            handlerRecord.Name = String.Format("add[@path='{0}']", CxmlExt);
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = string.Format("<add name=\"CXML\" verb=\"GET\" path=\"{0}\" type=\"{1}\" preCondition=\"integratedMode\"/>",
               CxmlExt, CxmlHandlerType);
            configModifications.Add(handlerRecord);


            string DzcHandlerType = typeof(DzcHandler).AssemblyQualifiedName;
            handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/handlers";
            handlerRecord.Name = String.Format("add[@path='{0}']", DzcExt);
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = string.Format("<add name=\"DZC\" verb=\"GET\" path=\"{0}\" type=\"{1}\" preCondition=\"integratedMode\"/>",
                 DzcExt, DzcHandlerType);
            configModifications.Add(handlerRecord);

            string DziHandlerType = typeof(DziHandler).AssemblyQualifiedName;
            handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/handlers";
            handlerRecord.Name = String.Format("add[@path='{0}']", DziExt);
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = string.Format("<add name=\"DZI\" verb=\"GET\" path=\"{0}\" type=\"{1}\" preCondition=\"integratedMode\"/>",
                 DziExt, DziHandlerType);
            configModifications.Add(handlerRecord);

            string DeepZoomImageHandlerType = typeof(DeepZoomImageHandler).AssemblyQualifiedName;
            handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/handlers";
            handlerRecord.Name = String.Format("add[@path='{0}']", DeepZoomImageHandlerPath);
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = string.Format("<add name=\"DeepZoomImage\" verb=\"GET\" path=\"{0}\" type=\"{1}\" preCondition=\"integratedMode\"/>",
                 DeepZoomImageHandlerPath, DeepZoomImageHandlerType);
            configModifications.Add(handlerRecord);

            string ImageTileHandlerType = typeof(ImageTileHandler).AssemblyQualifiedName;
            handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/handlers";
            handlerRecord.Name = String.Format("add[@path='{0}']", ImageTileHandlerPath);
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = string.Format("<add name=\"ImageTile\" verb=\"GET\" path=\"{0}\" type=\"{1}\" preCondition=\"integratedMode\"/>",
                 ImageTileHandlerPath, ImageTileHandlerType);
            configModifications.Add(handlerRecord);
            
            handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/staticContent";
            handlerRecord.Name = String.Format("mimeMap[@fileExtension='{0}']", ".cxml");
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = "<mimeMap fileExtension=\".cxml\" mimeType=\"text/cxml\" />";
            configModifications.Add(handlerRecord);

            handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/staticContent";
            handlerRecord.Name = String.Format("mimeMap[@fileExtension='{0}']", ".dzc");
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = "<mimeMap fileExtension=\".dzc\" mimeType=\"text/xml\" />";
            configModifications.Add(handlerRecord);

            handlerRecord = new SPWebConfigModification();
            handlerRecord.Path = "configuration/system.webServer/staticContent";
            handlerRecord.Name = String.Format("mimeMap[@fileExtension='{0}']", ".dzi");
            handlerRecord.Sequence = 0;
            handlerRecord.Owner = FeatureId;
            handlerRecord.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            handlerRecord.Value = "<mimeMap fileExtension=\".dzi\" mimeType=\"text/xml\" />";

            configModifications.Add(handlerRecord);

            return configModifications;
        }
    }
}
